<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form" >
      <a-form-item style="display:none">
        <a-input v-decorator="['deviceId']"/>
      </a-form-item>
      <a-form-item
        :labelCol="
          labelCol"
        :wrapperCol="wrapperCol"
        label="归属车站"
      >
        <a-tree-select
          v-decorator="['companyCode', {rules: [{ required: true, message: '请选择归属车站' }]}]"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="depts"
          placeholder="归属车站"
          treeDefaultExpandAll
        >
        </a-tree-select>
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备串号">
        <a-input placeholder="" v-decorator="['deviceId',{rules: [{required: true, message: '请输入设备串号'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备信息">
        <a-input placeholder="" v-decorator="['deviceMsg',{rules: [{required: true, message: '请输入设备信息'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备密码">
        <a-input placeholder="" v-decorator="['devicePwd',{rules: [{required: true, message: '请输入设备密码'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="程序版本">
        <a-input placeholder="" v-decorator="['deviceVersion',{rules: [{required: true, message: '请输入程序版本'}]}]"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveDevice } from '@/api/shifts/device'
import pick from 'lodash.pick'
import { getlistForShiftsDriverInfo } from '@/api/system'
export default {
  name: 'DeviceModal',
  props: {
    deptTree: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      depts: [],
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    getlistForShiftsDriverInfo().then(res => {
      this.buildtree(res.rows, this.depts, 115)
    })
  },
  methods: {
    add () {
      this.form.resetFields()
      this.edit({ deviceId: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'companyCode', 'deviceId', 'deviceMsg', 'devicePwd', 'deviceVersion'))
      })
    },
    buildtree (list, arr, parentId) {
      list.forEach(item => {
        if (item.parentId === parentId) {
          var child = {
            key: item.deptId,
            value: item.deptId + '',
            title: item.deptName,
            children: []
          }
          this.buildtree(list, child.children, item.deptId)
          arr.push(child)
        }
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          saveDevice(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
